package com.yungu.swift.admin.route.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.admin.AdminParam;
import com.yungu.swift.system.sys.model.dto.SysAssignConfigDto;
import com.yungu.swift.system.sys.model.param.AdminDispatchParam;
import com.yungu.swift.system.sys.service.SysAssignConfigService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.yungu.swift.admin.consts.AdminErrorEnum.PARAM_ERROR;

/**
 * @author : cuixiuyin
 * @date : 2019/6/17
 */
@RestController
@RequestMapping("/admin/v1/system/dispatch")
@Api(value = "【系统服务-业务设置中心】", tags = "派单配置")
public class DispatchAdmin {

    @Reference
    private SysAssignConfigService sysAssignConfigService;

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "派单配置信息获取", httpMethod = "GET")
    public ResponseData<SysAssignConfigDto> getAssignConfig(@RequestParam String districtUuid,
                                                            @RequestParam String businessType,
                                                            @RequestBody AdminParam adminParam) {
        Map<String, Object> map = MapUtils.build();
        map.put("districtUuid", districtUuid);
        map.put("businessType", businessType);
        map.put("appid", adminParam.getAppid());
        return sysAssignConfigService.get(map);
    }

    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "派单配置信息修改", httpMethod = "GET")
    public ResponseData<Boolean> editAssignConfig(@RequestBody AdminDispatchParam dispatchParam) {
        if (ParamUtil.isIllegal(dispatchParam.getUuid())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        SysAssignConfigDto sysAssignConfigDto = new SysAssignConfigDto();
        BeanUtils.copyProperties(dispatchParam, sysAssignConfigDto);
        sysAssignConfigDto.setUpdateBy(dispatchParam.getUserUuid());
        return sysAssignConfigService.edit(sysAssignConfigDto);
    }
}
